package edu.controller;

import edu.pojo.Member;
import edu.pojo.Sum;
import edu.service.EmailService;
import edu.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;

import javax.mail.MessagingException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private EmailService emailService;
    @Autowired
    private TemplateEngine templateEngine;


    /*
    * 添加报表
    * */
    @RequestMapping("/add")
    public String add(Member member, Model model) throws InterruptedException, MessagingException {
//        String phone = member.getPhone();

        System.out.println(member.getEmail());

        // 根据邮箱查询
        Sum sum = memberService.queryByEmail(member.getEmail());

        // 如果已存在,就直接返回
        if(sum.getCnt() != 0)
        {
            model.addAttribute("msg", "十分抱歉!该邮箱已经注册,如有疑问,请入群联系管理员!");
            model.addAttribute("jpg", "no.png");
            return "yes";
        }

        int res = memberService.addMember(member);

        if(res > 0)
        {
            // 查询自动生成的id
            int id = memberService.queryMemberByEmail(member.getEmail()).getId();
            member.setId(id);

            // 设置模板信息
            model.addAttribute("msg", "报名成功!");
            model.addAttribute("jpg", "yes.png");
            model.addAttribute("email", member.getEmail());

            // 发送thymeleaf邮件
            emailService.sendThymeleafMail("firstMeeting.html", member);
            return "yes";
        }
        else
        {
            model.addAttribute("msg", "十分抱歉!因为未知的原因报名失败,请重新填报!");
            model.addAttribute("jpg", "no.png");
            return "yes";
        }

    }


    /*@ResponseBody
    @GetMapping("/get")
    public String get() {
        Context context = new Context();

        // 设置其中的变量
        context.setVariable("name", "123");
        context.setVariable("id", "123");


        // 第一个参数是模板的名称
        String process = templateEngine.process("firstMeeting.html", context);
        return process;
    }*/

    /*
    * 测试邮箱请求
    * */
    @ResponseBody
    @GetMapping("/test")
    public String test() throws MessagingException {

        Member member = new Member();
        member.setEmail("321353225@qq.com");
        member.setName("杨金博");
        member.setId(999);

        emailService.sendThymeleafMail("hello.html", member);

        return "ok";
    }

    /*
    * 发送最终的结果函数
    * */
    @ResponseBody
    @RequestMapping("/sendlastmessage")
    public String sendLastMessage() {
        List<Member> members = memberService.queryAllMember();

        for (Member member : members) {
            if(member.getPass() == 1)
            {
                try {
                    emailService.sendThymeleafMail("hello.html", member);
                } catch (MessagingException e) {
                    System.out.println(member.getId());
                    e.printStackTrace();
                    return "fail";
                } finally {
                    System.out.println(member.getId());
                }
            }
            else
            {
                try {
                    emailService.sendThymeleafMail("sorry.html", member);
                } catch (MessagingException e) {
                    System.out.println(member.getId());
                    e.printStackTrace();
                    return "fail";
                } finally {
                    System.out.println(member.getId());
                }
            }
        }

        return "ok";
    }

    /*
    * 邮件补发
    * */
    @ResponseBody
    @RequestMapping("/remedy")
    public String remedy() throws MessagingException {
        int[] ids = null;

        List<Member> list = new ArrayList<>();

        for(int x : ids)
        {
            Member member = memberService.queryMemberByID(x);
            list.add(member);
        }

        for (Member member : list) {
            emailService.sendThymeleafMail("firstMeeting.html", member);
        }

        return "ok";
    }

}
